import { browser } from 'webextension-polyfill-ts';

import { chromeMessage } from '../../tool/chrome-message';
import { config } from '../../tool/config-manage';
import { logger } from '../../tool/log';
import { dateDiff } from '../../tool/tool';

export const autoUpdateInit = async () => {
    const conf = await config.get();
    if (!conf.autoUpdate) return;
    logger.log('自动更新');

    const { lastCheckTime } = await browser.storage.local.get('lastCheckTime');

    const time = new Date().getTime();
    /*
    * 不需要太频繁, 常用标签都汉化过了, 不常用的更新了也发现不了, 过多的请求可能会引起github的注意.
    * 每5天检查一次.
    * */
    const needCheck = (time - (1000 * 60 * 60 * 24 * 5)) > (lastCheckTime || 0);
    logger.log('上次自动更新检查', dateDiff(new Date(lastCheckTime)), new Date(lastCheckTime), needCheck ? '开始检查' : '跳过');
    if (needCheck) {
        await browser.storage.local.set({ lastCheckTime: time });
        const updated = await chromeMessage.send('auto-update', false);
        logger.log('自动更新结束', updated ? '有新版本并更新' : '没有新版本');
    }
};
